﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Fashion.Models
{
    public class ClothesRepository
    {
        //Query Methods
        //
        private FashionEntities FashionDB = new FashionEntities();

        public IQueryable<Product> FindAllProducts()
        {
            return FashionDB.Products;
        }

        //Return 10 newest product
        public IQueryable<Product> FindNewProducts()
        {
            var query = (from products in FashionDB.Products
                         orderby products.DateAdded descending
                         select products).Take(10);

            return query;
        }

        public Product GetProduct(int id)
        {
            return FashionDB.Products.SingleOrDefault(p => p.ProductID == id);
        }

        public IQueryable<Product> FindProductBySex(string sex)
        {
            var products = from p in FashionDB.Products
                           where p.Sex == sex
                           select p;
            return products;
        }

        public IQueryable<Product> FindProductByCategory(string sex, string category)
        {
            var products = from p in FashionDB.Products
                           where p.Category.Name == category &&
                           p.Sex == sex
                           select p;

            return products;
        }

        //Insert/Delete Methods
        //
        public void Add(Product product) 
        {
            FashionDB.AddToProducts(product);
        }

        //Persistance
        //
        public void Save()
        {
            FashionDB.SaveChanges();
        }
    }
}